package processor

import (
	"fmt"
	commonmodel "helloword01/31chatroom/common/model"
	"helloword01/31chatroom/common/utils"
	"helloword01/31chatroom/server/process"
	"net"
)

// 总的处理器

type Processor struct {
	Conn net.Conn
}

// ServerProcessMes
//
//	@Description:
//	@receiver this
//	@param mes
//	@return err
func (this *Processor) ServerProcessMes(mes *commonmodel.Message) (err error) {
	switch mes.Type {
	case commonmodel.LoginMesType: //登录
		up := &process.UserProcess{
			Conn: this.Conn,
		}
		err = up.ServerProcessLogin(mes)
	case commonmodel.RegisterMesType:
		up := &process.UserProcess{
			Conn: this.Conn,
		}
		err = up.ServerProcessRegister(mes)
	case commonmodel.SmsMesType:
		smsProcess := &process.SmsProcess{}
		smsProcess.SendGroupMes(mes)
	case commonmodel.SmsPrivateMesType:
		smsProcess := &process.SmsProcess{}
		smsProcess.SendPrivateMes(mes)
	case commonmodel.LogoutMesType:
		smsProcess := &process.SmsProcess{}
		smsProcess.SendLogoutMes(mes)

	default:
		fmt.Println("消息类型不存在")
	}
	return nil
}

func (this *Processor) Process() (err error) {
	for {
		tf := &utils.Transfer{
			Conn: this.Conn,
		}
		mes, err := tf.ReadPkg()
		if err != nil {
			return err
		}
		err = this.ServerProcessMes(&mes)
		if err != nil {
			return err
		}
	}
}
